/*
 * url.cpp
 *
 *  Created on: Aug 28, 2011
 *      Author: anonovermind <anonovermind@gmail.com>
 */

#include "../include/url.h"
#include "exceptions.h"

#include <boost/regex.hpp>

namespace Wishmaster2
{
namespace Network
{

class UrlImpl
{
public:
	UrlImpl() : m_valid(false)
	{
	}

	UrlImpl(const std::string& str) : m_valid(false)
	{
		parseString(str);
	}

	bool isValid() const
	{
		return m_valid;
	}

	void setScheme(const std::string& scheme)
	{
		m_scheme = scheme;
	}

	std::string scheme() const
	{
		return m_scheme;
	}

	void setHost(const std::string& host)
	{
		m_host = host;
	}

	std::string host() const
	{
		return m_host;
	}

	std::string path() const
	{
		return m_path;
	}

	void parseString(const std::string& str)
	{
		boost::regex rx("(?:([^:]+)://)?([^/]*)(?:(/.*))?$");
		boost::smatch results;
		if(boost::regex_match(str, results, rx))
		{
			m_scheme = results[1];
			m_host = results[2];
			m_path = results[3];
			if(m_path.empty())
				m_path = '/';
			m_valid = true;
		}
	}

private:
	bool m_valid;
	std::string m_host;
	std::string m_scheme;
	std::string m_path;
};

Url::Url() : m_impl(new UrlImpl)
{
}

Url::Url(const std::string& str) : m_impl(new UrlImpl(str))
{
}

Url::Url(const Url& url) : m_impl(new UrlImpl(*url.m_impl))
{

}

Url::~Url()
{
	delete m_impl;
}

Url& Url::operator=(const Url& url)
{
	m_impl = new UrlImpl(*url.m_impl);
	return *this;
}

bool Url::isValid() const
{
	return m_impl->isValid();
}

void Url::setScheme(const std::string& scheme)
{
	m_impl->setScheme(scheme);
}

std::string Url::scheme() const
{
	return m_impl->scheme();
}

void Url::setHost(const std::string& host)
{
	m_impl->setHost(host);
}

std::string Url::host() const
{
	return m_impl->host();
}

std::string Url::path() const
{
	return m_impl->path();
}

bool Url::operator==(const Url& url) const
{
	return true;
}


} /* namespace Network */
} /* namespace Wishmaster2 */
